package com.example.figurehecheng.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.figurehecheng.common.constant.MessageConstant;
import com.example.figurehecheng.common.properties.JwtProperties;
import com.example.figurehecheng.common.properties.WeChatProperties;
import com.example.figurehecheng.common.utils.HttpClientUtil;
import com.example.figurehecheng.common.utils.JwtUtil;
import com.example.figurehecheng.domain.dto.UserDTO;
import com.example.figurehecheng.domain.entity.User;
import com.example.figurehecheng.domain.vo.UserVO;
import com.example.figurehecheng.mapper.UserMapper;
import com.example.figurehecheng.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 宇科工作室
 * @since 2024-10-18
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    private final UserMapper userMapper;

    private final WeChatProperties weChatProperties;

    private final JwtProperties jwtProperties;

    //微信服务接口地址
    public static final String WX_LOGIN = "https://api.weixin.qq.com/sns/jscode2session";
    @Override
    public List<User> test() {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        List<User> list = userMapper.selectList(userQueryWrapper);
        return list;
    }

    /**
     * 用户登录
     *
     * @param userDTO
     * @return
     */
    @Override
    public UserVO userLogin(UserDTO userDTO) {
        //获取openid
        String openid = getOpenid(userDTO.getCode());
        if (ObjectUtil.isEmpty(openid)){
            throw new RuntimeException(MessageConstant.ACCOUNT_ERROR);
        }
        User user = lambdaQuery().eq(User::getOpenid, openid).one();
        if (!BeanUtil.isEmpty(user)){
            HashMap<String, Object> claims = new HashMap<>();
            claims.put("userId",user.getId());
            String jwt = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);
            UserVO userVO = new UserVO();
            userVO.setJwt(jwt);
            userVO.setName(user.getName());
            userVO.setHeadportain(user.getHeadportain());
            return userVO;
        }
            User users = BeanUtil.copyProperties(userDTO, User.class);
            users.setRegister(LocalDateTime.now());
            users.setOpenid(openid);
            save(users);
        return null;
    }
    /**
     * 调用微信接口服务，获取微信用户的openid
     * @param code
     * @return
     */
    private String getOpenid(String code){
        //调用微信接口服务，获得当前微信用户的openid
        Map<String, String> map = new HashMap<>();
        map.put("appid",weChatProperties.getAppid());
        map.put("secret",weChatProperties.getSecret());
        map.put("js_code",code);
        map.put("grant_type","authorization_code");
        String json = HttpClientUtil.doGet(WX_LOGIN, map);

        JSONObject jsonObject = JSON.parseObject(json);
        String openid = jsonObject.getString("openid");
        return openid;
    }
}
